﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Security.Permissions;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HostVirus1
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string file = @"C:\Windows\System32\Drivers\etc\hosts";
        private string myFile = @"D:\hosts";
        private string currentDir = @"C:\Windows\System32\Drivers\etc\";

        private void button1_Click(object sender, EventArgs e)
        {
            FileSecurity security = new FileSecurity();
            FileIOPermission permission = new FileIOPermission(FileIOPermissionAccess.Append, currentDir);

            permission.AddPathList(FileIOPermissionAccess.Append, currentDir);

            bool accessGranted = true;

            try
            {
                permission.Demand();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            //File.Replace(file,myFile,"hosts.bac");
            string str = textBox1.Text;
            using (StreamWriter writer = File.AppendText(file))
            {
                writer.WriteLine(str);
                writer.Flush();
            }
        }
    }
}
